const fs = require('fs')

// 文件操作
const newDir = __dirname + '/fsdir';
if (fs.existsSync(newDir)) {
    console.log('dir is exists,delete dir!')
    const newDirFiles = fs.readdirSync(newDir);
    if (newDirFiles && newDirFiles.length > 0) {

    } else {
        fs.rmdirSync(newDir);
    }
    const currentFiles = fs.readdirSync(__dirname);
    console.log('query current dir file list:')
    currentFiles && currentFiles.forEach(file => {
        console.log(file)
    })
} else {
    fs.mkdirSync(newDir)
}
const userFile = newDir + '/user.json';
console.log(userFile)
if (fs.existsSync(userFile)) {
    console.log('user.json file is exists!')
    const f = `${newDir}/${Date.now()}.json`
    const fcp = `${newDir}/${Date.now()}cp.json`
    const cpf = fs.copyFileSync(userFile, fcp);
    const rnf = fs.renameSync(userFile, f);
    const fileBuff = fs.readFileSync(f);
    console.log(fileBuff.toString())
    console.log("===cpf===",cpf)
    console.log("===rnf===",rnf)
} else {
    fs.writeFileSync(userFile, '{"name":"json"}');
}
// 文件监控

// 文件流